package cn.dnf.pet.service.impl;

import cn.dnf.basic.service.impl.BaseServiceImpl;
import cn.dnf.pet.domain.Pet;
import cn.dnf.pet.domain.PetDetail;
import cn.dnf.pet.mapper.PetDetailMapper;
import cn.dnf.pet.mapper.PetMapper;
import cn.dnf.pet.service.IPetDetailService;
import cn.dnf.pet.service.IPetService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
   @Autowired
   private PetMapper petMapper;

   @Autowired
   private PetDetailMapper petDetailMapper;
    @Override
    public void save(Pet pet) {
        //先添加pet,返回自增的主键
        petMapper.save(pet);

        //再添加pet_detail
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.save(petDetail);
    }

    @Override
    public void update(Pet pet) {
        //pet
        petMapper.update(pet);
        //detail
        petDetailMapper.update(pet.getPetDetail());
    }

    @Override
    public void onSale(List<Long> ids) {
        //1.审核,通过则上架,没通过直接自定义异常,保存一条数据

        //2.改状态
        //上架时间
        Date date = new Date();
        petMapper.onSale(ids,date);
    }

    @Override
    public void offSale(List<Long> ids) {
        /**
         * 改状态 slq中写state =1
         * 改下架时间: 创建一个当前时间,数据库修改
         * 1.对象
         * 2.map
         * 3.直接写两个参数
         */
        Date date = new Date();

        petMapper.offSale(ids,date);
    }

    @Override
    public void remove(Long id) {

        super.remove(id);
        //详情
        petDetailMapper.removeByPetId(id);
    }
}
